#include "stdio.h"
#include "windows.h"
#include "time.h"

void generateAndWriteGraph(char* filename, int n, int m, int symm)
{
	int maxEdges = (n*(n-1))/* /2 */; //directed
	if (symm)
		maxEdges/=2;
	if (m>maxEdges)
		m=maxEdges;
	srand(time(NULL));
	int* adjM = new int[n*n];
	memset(adjM,-1,n*n*sizeof(int));
	for (int i=0; i<n; i++)
		adjM[i*(n+1)]=0;

	int addedEdges=0;
	while (addedEdges<m)
	{
		int v1 = rand()%n;
		int v2 = rand()%n;
		int w = rand()+1;
		if (adjM[v1*n+v2]<0)
		{
			adjM[v1*n+v2] = w;
			if (symm)
				adjM[v2*n+v1] = w;
			addedEdges++;
		}
	}

	FILE* outFile = fopen(filename, "w");
	fprintf(outFile, "%i %i\n", n, symm);
	for (int i=0; i<n; i++)
		for (int j = symm ? i : 0 ; j<n; j++)
		{
			if (adjM[i*n+j]>0)
				fprintf(outFile, "%i %i %i\n", i+1, j+1, adjM[i*n+j]);
		}
		fclose(outFile);

		delete[] adjM;

}

int main(int argc, char** argv)
{
	int n, m, symm;
	if (argc==4)
	{
		n = atoi(argv[1]);
		m = atoi(argv[2]);
		symm = atoi(argv[3]);
	}
	else
		scanf("%i %i %i", &n, &m, &symm);
	char filename[255];
	if (n>0 && m>0 && (symm==0 || symm==1))
	{
		sprintf(filename, "graph_%i_%i_%i.txt", n,m,symm);
		generateAndWriteGraph(filename,n,m,symm);
	}

	return 0;
}